\makeatletter

\newcommand{\myspinewidth}{13mm}
\newcommand{\umfonta}{\fontsize{14.55}{14.55}\selectfont}
\newcommand{\umfontb}{\fontsize{17}{17}\selectfont}
\newcommand{\umfontc}{\fontsize{10}{10}\selectfont}

\newdata{logo}
\logo(0):={\theuniversity(logo,RGB)}
\logo(1):={\theschool(logo,RGB)}
% \logo(2):={\theuniversity(logo,RGB)}

\school(1):={\theschool(pt)}
% \school(2):={\theuniversity(pt)}

\spine(title):={\thedoctitle(\@LANG@COVER,main)}

% \color{\thethesiscover(\option{/novathesis/doctype},1-1,textcolor)}
\datamatchtf{\match}{thesiscover}(%
            {\@DOCTYPE,\@DEGREEACR,1-1,textcolor}%
           ,{\@DOCCLASS,\@DEGREEACR,1-1,textcolor}%
           ,{\@DEGREEACR,1-1,textcolor}%
           ,{\@DEGREEACR,textcolor}%
           ,{\@DOCTYPE,1-1,textcolor}%
           ,{\@DOCCLASS,1-1,textcolor}%
           ,{1-1,textcolor}%
           ,{textcolor}%
){\color{\match}}{}%
\datamatch{\@UM@BGCOLOR@i}{thesiscover}(%
            {\@DOCTYPE,\@DEGREEACR,1-1,bgcolor}%
           ,{\@DOCCLASS,\@DEGREEACR,1-1,bgcolor}%
           ,{\@DEGREEACR,1-1,bgcolor}%
           ,{\@DEGREEACR,bgcolor}%
           ,{\@DOCTYPE,1-1,bgcolor}%
           ,{\@DOCCLASS,1-1,bgcolor}%
           ,{1-1,bgcolor}%
           ,{bgcolor}%
)%
\datamatch{\@UM@BGCOLOR@ii}{thesiscover}(%
            {\@DOCTYPE,\@DEGREEACR,1-2,bgcolor}%
           ,{\@DOCCLASS,\@DEGREEACR,1-2,bgcolor}%
           ,{\@DEGREEACR,1-2,bgcolor}%
           ,{\@DEGREEACR,bgcolor}%
           ,{\@DOCTYPE,1-2,bgcolor}%
           ,{\@DOCCLASS,1-2,bgcolor}%
           ,{1-2,bgcolor}%
           ,{bgcolor}%
)%

\let\oldstockwidth=\stockwidth
\begin{newpdflayout}[%
  \settrims{0pt}{0pt}%
  \setlrmarginsandblock{0pt}{*}{*}%
  \setulmarginsandblock{0pt}{*}{*}%
  \setlength{\parskip}{0pt}%
  \setheadfoot{0pt}{0pt}%
  \setheaderspaces{*}{0pt}{*} %
  \setmarginnotes{0pt}{0pt}{0pt}%
]{\stockheight}{2\stockwidth+\myspinewidth}%
  % \noindent% \Huge HELLO
\begin{tikzpicture}[remember picture, overlay, transform shape]
  % BACKGROUND
  \node [anchor=north west, inner sep=0pt]
    at (current page.north west) {
      \begin{tikzpicture}
          \filldraw [fill=\expanded{\@UM@BGCOLOR@i}] (0,0) rectangle (\stockwidth,-\stockheight);
      \end{tikzpicture}
    };
  % FRONT COVER — RIGHT PAGE
  \node [anchor=north west, inner sep=0pt, xshift=0.5*\myspinewidth]
    at (current page.north) {
      \begin{tikzpicture}[remember picture, overlay, xshift=7.9cm]
        \node [anchor=west, inner sep=0pt]
            at (0,-3.825)
              {\umfonta\bfseries\theuniversity(pt)};
        \foreach \myi in {0,...,4} {
            \node [anchor=north west, inner sep=0pt]
              at (\myi*2.6,0)
                  {\ifdatadefined{logo}(\myi){%
                    \includegraphics[width=2.6cm,align=t]{\thelogo(\myi)}%
                  }{}};
            \node [anchor=north west, inner sep=0pt]
              at (0,-3.625-\myi*0.6)
                  {\ifdatadefined{school}(\myi){%
                    \umfonta\theschool(\myi)%
                  }{}};
        }
        \node [anchor=north west, inner sep=0pt]
            at (0,-9)
              {\umfontb\thedocauthor(name)};
        \node [anchor=north west, inner sep=0pt]
            at (0,-10.45)
              {\umfontb\bfseries\parbox{111mm}{\thedoctitle(\@LANG@COVER,main,cover)}};
        \node [anchor=south west, inner sep=0pt]
            at (0,-28.7)
              {\umfontc
              \printisodate[\theofstr(\@LANG@COVER)]
                           {\thentdocdate(submission,year)-\thentdocdate(submission,month)}};
      \end{tikzpicture}
    };
  % SPINE
  \node [anchor=north, inner sep=0pt]
    at (current page.north) {
      \begin{tikzpicture}[remember picture, overlay]
        \foreach \myi in {0,...,4} {
            \node [anchor=north west, inner sep=0pt, xshift=-0.5*\myspinewidth]
              at (0,-\myi*\myspinewidth)
                  {\ifdatadefined{logo}(\myi){%
                    \includegraphics[width=\myspinewidth,align=t]{\thelogo(\myi)}%
                  }{}};
        }
        \node [anchor=east, inner sep=0pt, rotate=90]
            at (0,-9)
              {\thespine(font)\parbox{7.75cm}{\raggedright\bfseries\thespine(title)}};
        \node [anchor=east, inner sep=0pt, rotate=90]
            at (0,-17.05)
              {\thespine(font)\thespine(author)};
        \node [anchor=west, inner sep=0pt, rotate=90]
            at (0,-28.7)
              {\thespine(font)UMinho $|$ \thespine(year)};
      \end{tikzpicture}
    };
  % BACK COVER — LEFT PAGE
  \node [anchor=south west, inner sep=1cm]
    at (current page.south west) {
      \begin{minipage}{19cm}
          \foreach \myi in {1,...,9} {%
            \ifdatadefined{sponsor}(\@LANG@COVER,\myi,logo){%
              \thesponsor(\@LANG@COVER,\myi,logo)\qquad%
            }{}%
          }%
      \end{minipage}
    };
\end{tikzpicture}

\newpage

\begin{tikzpicture}[remember picture, overlay, transform shape]
  % BACKGROUND
  \node [anchor=north west, inner sep=0pt]
    at (current page.north west) {
      \begin{tikzpicture}
          % \node [inner sep=0pt]
          %   at (0,0)
          %       {\includegraphics[width=\stockwidth,height=\stockheight,align=t]{cover0}};
          %       %{CAPA-Tese-Doutoramento-EA}};
          \filldraw [fill=\expanded{\@UM@BGCOLOR@ii}] (0,0) rectangle (\stockwidth,-\stockheight);
      \end{tikzpicture}
    };
\end{tikzpicture}

\end{newpdflayout}

